﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using xTeam.HengLe.Entity;
using Valor.Helper;
using System.Globalization;
using ShopB2C.Entity;
using System.ComponentModel;
using System.Web.Security;
using xTeam.HengLe.Helper;
using System.IO;

namespace xTeam.HengLe.Controller.Website
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            return View();
            // return RedirectToAction("Index", "Product");
        }

        public ActionResult Menu(string moduleKey = "")
        {
            List<EClassify> classifyList = _classifyDataAccess.GetClassLisytByModuleKey(moduleKey);
            return View(classifyList);
        }
        public ActionResult Thumb(int w = 300, int h = 100, string s = "")
        {
            w = w <= 1 ? 300 : w;
            h = h <= 1 ? 100 : h;

            if (System.IO.File.Exists(Server.MapPath(s)))
            {
                Response.ContentType = "image/jpeg";
                System.IO.MemoryStream ms = ThumbImageHelper.CuteThumbPhoto(w, h, Server.MapPath(s), false, true);
                Response.BinaryWrite(ms.ToArray());
            }


            return new EmptyResult();
        }

        public ActionResult Register()
        {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.Register);

            return View();
        }

        [HttpPost]
        public ActionResult Register(UserEntity registerUser)
        {
            MessageHelper msgHelper = new MessageHelper("register");
            // 验证用户名不能为空
            msgHelper.Required(registerUser.Uname, 1);
            // 验证EMAIL格式不正确
            msgHelper.Email(registerUser.Uname, 2);
            // 验证用户昵称不能为空
            msgHelper.Required(registerUser.NickName, 4);
            // 验证密码不得小于6位
            msgHelper.MinLength(registerUser.Password, 6, 3);
            // 所有检查成功开始注册
            if (msgHelper.IsValid)
            {
                if (_userAccess.RegNewUser(registerUser) < 0)
                {
                    msgHelper.AddBody(1);
                    msgHelper.DefaultOperate(Url.Action("Login", "Home"));
                    msgHelper.AddMessage(5);
                }
                else
                {
                    msgHelper.AddMessage(6);
                }
            }
            else
            {
                msgHelper.AddBody(2);
                msgHelper.AddOperate("登陆", Url.Action("Login", "Home"));
                msgHelper.AddOperate("首页", Url.Action("Index", "Home"));
            }

            return MessageResult(msgHelper.CreateMessage());

        }

        public ActionResult Login()
        {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.Login);

            return View();
        }

        public ActionResult RandImage()
        {
            RandomHelper.OutputRadomImage(this.HttpContext, RandomHelper.RandomString(4), "RandImageCode", null);
            return new EmptyResult();
        }

        /// <summary>
        /// 登录
        /// </summary>
        [HttpPost]
        public ActionResult Login(
            [DefaultValue("")]string uname
            , [DefaultValue("")]string password
            , [DefaultValue("")]string varifyCode
            , [DefaultValue(0)]int timeout
            , [DefaultValue("/")]string returnUrl)
        {
            MessageHelper msg = new MessageHelper("login");
            UserEntity user = _userAccess.Login(uname, password);
            msg.AddBody(1);

            
            string rand = Session["RandImageCode"].ToString();
            if (string.IsNullOrEmpty(varifyCode) || rand != SecurityHelper.SHA256Hash(varifyCode))
            {
                msg.AddMessage(2);
            }
            else
            {
                msg.AddMessage(1);
                if (!object.Equals(user, null))
                {
                    this.WriteLoginCookie(user, timeout);
                    return Redirect(returnUrl);
                }
            }

            return MessageResult(msg.CreateMessage());

        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }


        public ActionResult ForgetPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgetPassword(int? i)
        {
            return View();
        }

        public ActionResult RecordPassword()
        {
            return View();
        }
        [HttpPost]
        public ActionResult RecordPassword(int? i)
        {
            return View();
        }
    }

}
